<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" ref="form" :model="form" class="demo-form-inline">
            <el-form-item prop="name" label="资源名称">
                <el-input v-model="form.name" placeholder="请输入资源名称"></el-input>
            </el-form-item>
            <el-form-item prop="url" label="资源路径">
                <el-input v-model="form.url" placeholder="请输入资源路径"></el-input>
            </el-form-item>
            <el-form-item prop="categoryId" label="资源分类">
                <el-select
                    v-model="form.categoryId"
                    placeholder="请选择资源分类"
                    clearable
                >
                    <el-option
                        v-for="item in resourceCategories"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
                <el-button @click="onReset" :disabled="isLoading">重置</el-button>
            </el-form-item>
        </el-form>
      </div>
      <div slot="header" class="clearfix">
        <el-button
          size="mini"
          @click="onAdd()">添加</el-button>
        <el-button
          size="mini"
          @click="goMenuPage()">资源分类</el-button>
      </div>
      <el-dialog
        :title="isEdit ? '编辑资源' : '添加资源'"
        :visible.sync="dialogVisible"
        width="500px"
        :before-close="handleClose"
        >
        <el-form
          ref="add"
          :rules="addRules"
          :model="add"
          label-width="100px"
          label-position="right"
        >
          <el-form-item prop="name" label="资源名称:">
            <el-input v-model="add.name" size="mini"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径:">
            <el-input v-model="add.url" size="mini"></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类:">
                <el-select
                    v-model="add.categoryId"
                    placeholder="请选择资源分类"
                    clearable
                    size="mini"
                >
                    <el-option
                        v-for="item in resourceCategories"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
          <el-form-item prop="description" label="描述:">
            <el-input type="textarea" v-model="add.description"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleAddResource">确 定</el-button>
        </span>
      </el-dialog>
      <el-table
        :data="resources"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
      >
        <el-table-column
            type="index"
            label="编号"
        >
        </el-table-column>
        <el-table-column
            prop="name"
            label="资源名称"
        >
        </el-table-column>
        <el-table-column
            prop="url"
            label="资源路径"
        >
        </el-table-column>
        <el-table-column
            prop="description"
            label="描述"
        >
        </el-table-column>
        <el-table-column
            prop="createdTime"
            label="添加时间"
        >
        </el-table-column>
        <el-table-column
            label="操作"
        >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :disabled="isLoading"
        :current-page.sync="form.current"
        :page-sizes="[5, 10, 20]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
        </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourcePages, createOrUpdateResource } from '@/services/resource'
import { getResourceCategories } from '@/services/resource-category'
import { Form } from 'element-ui'
import { getTime } from '@/utils/time'

export default Vue.extend({
  name: 'ResourceList',
  data () {
    return {
      isEdit: false,
      resources: [], // 资源列表
      resourceCategories: [], // 资源分类
      form: {
        name: '',
        url: '',
        categoryId: null, // 资源分类id
        current: 1, // 请求的页码
        size: 10 // 默认每页数据量
      },
      add: {
        id: null,
        name: '',
        url: '',
        categoryId: null, // 资源分类id
        description: ''
      },
      addRules: {
        name: [
          { required: true, message: '请输入资源名称', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入资源路径', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '请选择资源分类', trigger: 'blur' }
        ]
      },
      totalCount: 0,
      isLoading: true,
      dialogVisible: false
    }
  },
  methods: {
    onSubmit () {
      // 筛选查询从第一页开始
      this.form.current = 1
      this.loadResources()
    },
    async loadResources () {
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.resources = data.data.records.map((item: any) => {
          item.createdTime = getTime(item.createdTime)
          return item
        })
        this.totalCount = data.data.total
      }
      this.isLoading = false
    },
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      this.resourceCategories = data.data
    },
    handleEdit (item: any) {
      this.isEdit = true
      this.add = item
      this.dialogVisible = true
    },
    handleDelete () {
      console.log('handleDelete')
    },
    handleSizeChange (val: number) {
      // 修改每页大小
      this.form.size = val
      // 每页大小改变重新查询第一页数据
      this.form.current = 1
      // 重新请求数据
      this.loadResources()
    },
    handleCurrentChange (val: number) {
      // 修改要查询的页码
      this.form.current = val
      // 重新请求数据
      this.loadResources()
    },
    onReset () {
      (this.$refs.form as Form).resetFields()
      // 重置到第一页
      this.form.current = 1
      this.loadResources()
    },
    onAdd () {
      this.isEdit = false
      this.dialogVisible = true
    },
    goMenuPage () {
      this.$router.push({
        name: 'menu'
      })
    },
    async handleClose () {
      await (this.$refs.add as Form).resetFields()
      this.dialogVisible = false
    },
    async handleAddResource () {
      try {
        // 1. 表单验证
        await (this.$refs.add as Form).validate()
        // 2. 添加资源
        await createOrUpdateResource(this.add)
        this.handleClose()
        this.loadResources()
      } catch (error) {
        console.log(error)
      }
    }
  },
  created () {
    this.loadResources()
    this.loadResourceCategories()
  }
})
</script>

<style lang="scss" scoped>
</style>
